package com.happy_lab_management.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PasswordUtil {
    public static String encryptPassword(String password) {
        try {
            // 第一次MD5加密
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes());
            byte[] digest = md.digest();

            // 转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : digest) {
                hexString.append(String.format("%02x", b & 0xff));
            }
            String firstMD5Hash = hexString.toString();

            // 反转字符串
            String reversedString = new StringBuilder(firstMD5Hash).reverse().toString();

            // 第二次MD5加密
            md.update(reversedString.getBytes());
            digest = md.digest();

            // 转换为十六进制字符串
            hexString = new StringBuilder();
            for (byte b : digest) {
                hexString.append(String.format("%02x", b & 0xff));
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
           throw new RuntimeException();
        }
    }
}
